<template>
	<view class="pw">
		<UploadFile :uploadType="item.uploadType"  v-for="(item,index) in fileListInfo" :key="index" @uploadComplete="uploadFileProcess" :deliveryReceiptList="deliveryReceiptList">{{fileTitle(item.uploadType)}}上传</UploadFile>
		<file-pull :fileType="item.type" :fileName="item.fileName"
		 :saveCapacity="item.fileSize" :pageNum="9" :process="item.process" 
		  :keyId="index" @cancelTask="deleteFile" v-for="(item,index) in processer.processPingList[documentType]"  :key="index"/>
	</view>
</template>

<script setup>
	// 文件上传容器
	import {defineProps } from 'vue'
	import {useProcessStore} from '@/store/index.js'
	import {formatFileSize} from '@/utils/commons.js'
	import {uploadFile} from '@/api/upload.js'
	const processer = useProcessStore()
	const props = defineProps({
		fileListInfo:{
			type:Array,
			default:()=>[]
		},
		documentType:{
			type:Number,
			required:true
		},
		deliveryReceiptList:{
			type:Array,
			default:()=>[]
		}
	})
	// 上传文件
	const uploadFileProcess = async(res,type)=>{
		let fileType = ""
		let fileSize = ""
		let fileName = ""
		let filePath = ""
		switch(type){
			case "file":
			const {name,path,size,type} = res[0]
			 fileType = type.split('/').pop()
			 fileSize = formatFileSize(size)
			 fileName = name
			 filePath = path
			break;
			case "image":
			const {tempFilePaths,tempFiles} = res
			const fileInfo = tempFiles[0]
			 fileType = fileInfo.type.split('/').pop()
			 fileSize = formatFileSize(fileInfo.size)
			 fileName = fileInfo.name
			 filePath = tempFilePaths[0]
			break;
		}
		const fileWrap =await middleWear(filePath,fileName,fileType,fileSize,props.documentType)
		if(fileWrap){
		  	console.log("发送成功");
		}else{
			console.log("发送失败");
		}
	}
		//上传中间件处理
	const middleWear = async(tempFilePaths,fileName,type,fileSize,sendType)=>{
		processer.addChangeProcess({
					fileName,
					type,
					fileSize,
					process:0,
				},sendType)
		const fileWrap = await uploadFile(tempFilePaths,processer.processPingList[sendType].length -1,sendType)
		const {url,ossId} = fileWrap
		processer.processPingList[sendType][processer.processPingList[sendType].length -1]["url"] = url
		processer.processPingList[sendType][processer.processPingList[sendType].length -1]["ossId"] = ossId
		return fileWrap
	}
	//删除取消上传文件
	const deleteFile = (val)=>{
		processer.deleteProcess(props.documentType,val)
	}
	const fileTitle = (type)=>{
		const enmu = [
			{title:"文件",type:"file"},
			{title:"图片",type:"image"},
		]
		return enmu.reduce((str,val)=>{
			if(type === val.type){
				str+=val.title
			}
			return str
		},"")
	}
</script>

<style lang="scss" scoped>
.pw{
	
}
</style>